package com.tb.controller;

import com.tb.DTO.ResetPasswordDTO;
import com.tb.DTO.userDTO;
import com.tb.Service.UserService;
import com.tb.entity.user;
import com.tb.properties.JwtProperties;
import com.tb.result.Result;
import com.tb.vo.userVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.tb.constant.JwtClaimsConstant;
import java.util.HashMap;
import java.util.Map;
import com.tb.utils.JwtUtil;
import com.tb.properties.JwtProperties;
import java.util.List;
import java.util.stream.Collectors;

import javax.security.auth.login.AccountNotFoundException;

@CrossOrigin("*")
@RestController
@RequestMapping("/auth")
@Slf4j
@Api("用户登录功能")
public class userController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    /*
     * 用户登录
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<userVo> login(@RequestBody userDTO userDTO) {
        log.info("用户登录");
        try {
            user user = userService.login(userDTO);
            // 登录成功后，生成jwt令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put(JwtClaimsConstant.EMP_ID, user.getId());
            String token = JwtUtil.createJWT(
                    jwtProperties.getAdminSecretKey(),
                    jwtProperties.getAdminTtl(),
                    claims);

            userVo userLoginVO = userVo.builder()
                    .id(user.getId())
                    .userName(user.getUsername())
                    .token(token)
                    .build();

            return Result.success(userLoginVO);
        } catch (AccountNotFoundException e) {
            return Result.error(e.getMessage());
        }
    }

    /*
     * 用户退出
     */
    @PostMapping("/logout")
    @ApiOperation("用户退出")
    public Result<String> logout() {
        return Result.success();
    }

    /*
     * 注册用户
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result register(@RequestBody userDTO userDTO) {
        log.info("用户注册");
        userService.register(userDTO);
        return Result.success();
    }

    /*
     * 修改密码
     */
    @PostMapping("/reset-password")
    @ApiOperation("修改密码")
    public Result resetPassword(@RequestBody ResetPasswordDTO resetPasswordDTO) {
        log.info("修改密码");
        userService.resetPassword(resetPasswordDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("获取用户信息")
    public Result<userVo> getUserInfo(@PathVariable("id") Long id) {
        log.info("根据id查询用户:{}", id);
        user user = userService.getById(id);
        if (user != null) {
            com.tb.vo.userVo userVo = com.tb.vo.userVo.builder()
                    .id(user.getId())
                    .userName(user.getUsername())
                    .build();
            return Result.success(userVo);
        }
        return Result.error("用户不存在");
    }

    @GetMapping("/list")
    @ApiOperation("获取所有用户信息列表")
    public Result<List<userVo>> getUserList() {
        log.info("获取所有用户信息列表");
        List<user> userList = userService.getUserList();
        List<userVo> userVoList = userList.stream()
                .map(user -> userVo.builder()
                        .id(user.getId())
                        .userName(user.getUsername())
                        .build())
                .collect(Collectors.toList());
        return Result.success(userVoList);
    }
}
